package com.wang.rabbitmq.mqtest;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author: wang
 * @date: 2021/8/2 20:26
 * @description:
 */
public class MyDefaultReceive {
    public static void main(String[] args) throws IOException, TimeoutException {
        defaultReceive();
    }

    private static void defaultReceive() throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setPort(5672);
        factory.setHost("192.168.169.123");
        factory.setVirtualHost("/");
        factory.setUsername("root");
        factory.setPassword("root");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.basicConsume("baseQueue", true,  new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者的唯一标识: "+consumerTag);
                System.out.println("消息的唯一标识："+envelope.getDeliveryTag());
                System.out.println("交换机："+envelope.getExchange());
                System.out.println("路由键："+envelope.getRoutingKey());
                System.out.println("传递过来的消息: "+new String(body));
                System.out.println("===================================");
            }
        });
    }
}
